/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DAO;

import java.util.ArrayList;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.Query;
import model.Security;

/**
 *
 * @author William
 */
public class SecurityDao {
    public void create(Security security, EntityManager em) {
        try {
            em.persist(security);
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }

    public Security search(Long id, EntityManager em) {
        Security security = null;
        try {
            security = em.find(Security.class, id);
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return security;
    }

    public void update(Security security, EntityManager em) {
        try {
            em.merge(security);
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }

    public void delete(Security security, EntityManager em) {
        try {
            em.remove(security);
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }

    public List<Security> listAll(EntityManager em) {
        List<Security> securityList = new ArrayList<>();
        Query q = em.createQuery("SELECT u FROM Security u");
        try {
            List<Security> securitys = new ArrayList<Security>();
            securitys = q.getResultList();
            for (Object s : securitys) {
                securityList.add((Security) s);
            }
            return securityList;
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return securityList;
    }
    
}
